package net.gaox.sponsor.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import net.gaox.common.api.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * <p> 测试接口 </p>
 *
 * @author gaox·Eric
 * @date 2024-04-10 20:29
 */
@RestController
public class SponsorTestController {

    @GetMapping("/random")
    @HystrixCommand(fallbackMethod = "hystrixFallback")
    public ApiResponse hystrix() {
        Random random = new Random();
        int value = Math.abs(random.nextInt(100));
        if (0 == value % 3) {
            throw new RuntimeException("error");
        }
        ApiResponse success = ApiResponse.success();
        success.data(value);
        return success;
    }

    /**
     * 熔断器
     *
     * @param e 异常
     * @return 熔断响应
     */
    public ApiResponse hystrixFallback(Throwable e) {
        return ApiResponse.fail("接口不能正常工作");
    }

}
